package com.example.jigsawpuzzles.slice;

import com.example.jigsawpuzzles.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Image;
import ohos.agp.components.TableLayout;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

public class SelectImageSlice extends AbilitySlice {
    int colNum = 2;   // 每列展示的图片数量
    int imageNum = 4;   // 图片总数
    int gameSize = 1080;    // 界面宽度
    TableLayout tableLayout;    // 选图界面主布局
    ArrayList<Integer> drawables;   // 存储图片资源
    Image[] images; // 界面中的图片的实体类

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_select_image);

        // 读取ResourceTable中的图片
        try {
            drawables = getMediaList();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取图片数量
        imageNum = drawables.size();
        // 初始化组件
        images = new Image[imageNum];

        tableLayout = (TableLayout) findComponentById(ResourceTable.Id_select_image);

        // 设置视图行列信息
        tableLayout.setRowCount((imageNum + 1) / 2);
        tableLayout.setColumnCount(colNum);

        // 设置每一张图片的信息
        for (int i = 0; i < imageNum; i++) {
            images[i] = new Image(getContext());
            images[i].setPixelMap(drawables.get(i));

            // 设置宽度和高度
            images[i].setHeight(gameSize / colNum);
            images[i].setWidth(gameSize / colNum);
            
            // 格式化pixelMap
            PixelMap pixelMap = images[i].getPixelMap();
            ImageInfo imageInfo = pixelMap.getImageInfo();

            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.pixelFormat = imageInfo.pixelFormat;
            options.editable = true;
            options.size = new Size(gameSize / colNum, gameSize / colNum);

            pixelMap = PixelMap.create(pixelMap, new Rect(0, 0, imageInfo.size.width, imageInfo.size.height), options);
            images[i].setPixelMap(pixelMap);


            int finalI = i;
            images[i].setClickedListener(component -> {
                //跳转到哪个页面
                Intent intentNext = new Intent();
                intentNext.setParam("selectDiff", intent.getIntParam("selectDiff", 0));
                intentNext.setParam("selectImg", finalI);

                // 包含了要跳转的页面信息
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")// 跳转到哪个页面上，如果一个没有内容的字符串，则表示跳转本机
                        .withBundleName("com.example.jigsawpuzzles")// 跳转到哪个应用上
                        .withAbilityName("com.example.jigsawpuzzles.jigsaw")// 要跳转的页面
                        .build();

                //把打包后的operation设置到意图中
                intentNext.setOperation(operation);

                //跳转页面
                startAbility(intentNext);
            });
            // 将图片加入至视图中
            tableLayout.addComponent(images[i]);

        }
    }

    public static ArrayList<Integer> getMediaList() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        // 获取ResourceTable类
        Class<ResourceTable> drawableClass = ResourceTable.class;
        ResourceTable instance = (ResourceTable) drawableClass.getDeclaredConstructors()[0].newInstance();
        ArrayList<Integer> drawables = new ArrayList<>();
        // 遍历drawable中的属性
        for (Field field : drawableClass.getDeclaredFields()) {
        // 匹配media标头
            if (field.getName().startsWith("Media_")) {
                int value = field.getInt(instance);
                drawables.add(value);
            }
        }
        return drawables;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onInactive() {
        // 如果此ability进入后台，则直接销毁
        terminate();
    }
}
